package string_question.leecode.test6;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/6 17:19
 */
public class Solution1 {

    // 方法一：直接按照指针进行求解
    public String convert(String s, int numRows) {
        if (s == null || s.length() < 3 || numRows == 1) return s;
        StringBuilder ans = new StringBuilder();
        char[] chars = s.toCharArray();
        int n = chars.length;
        int step = 2 * (numRows - 1);
        int c = 0;
        for (int i = 0; i < numRows; i++) {
            if (i == 0) {
                c = 0;
                while (c < n) {
                    ans.append(chars[c]);
                    c += step;
                }
            } else if (i == numRows - 1) {
                c = i;
                while (c < n) {
                    ans.append(chars[c]);
                    c += step;
                }
            } else {
                c = 0;
                while (c + i < n) {
                    ans.append(chars[c + i]);
                    c += step;
                    if (c - i < n) {
                        ans.append(chars[c - i]);
                    }
                }
            }
        }
        return ans.toString();
    }
}
